package com.gw.quotes.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gw.quotes.AppInfo;
import com.gw.quotes.R;
import com.gw.quotes.entity.business.GetGoodsTopNEntity.Pricedetail;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.yxw.view.util.ResFinder;

public class GridAdapter extends BaseAdapter {

	private Context context;
	private int resource;
	private ArrayList<Pricedetail> list;

	private DisplayImageOptions options;
	protected ImageLoader imageLoader;

	public GridAdapter(Context context, int layoutId,
			ArrayList<Pricedetail> list) {
		this.context = context;
		this.resource = layoutId;
		this.list = list;
		this.imageLoader = ImageLoader.getInstance();
		this.imageLoader.init(ImageLoaderConfiguration.createDefault(context));
		this.options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_empty)
				.showImageForEmptyUri(
						ResFinder.getR(context, "R.drawable.ic_empty"))
				.showImageOnFail(ResFinder.getR(context, "R.drawable.ic_empty"))
				.cacheInMemory(true).cacheOnDisk(true)
				.displayer(new FadeInBitmapDisplayer(500))// 是否图片加载好后渐入的动画时间
				.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	@Override
	public int getCount() {
		return this.list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Pricedetail gridData = list.get(position);
		LinearLayout layout = null;
		if (convertView == null) {
			layout = (LinearLayout) LayoutInflater.from(this.context).inflate(
					resource, null);
		} else {
			layout = (LinearLayout) convertView;
		}
		ImageView img = (ImageView) layout.findViewById(R.id.grid_img);
		img.getLayoutParams().height=(int) (AppInfo.SCREEN_WIDTH/2.3);
		TextView title = (TextView) layout.findViewById(R.id.grid_title);
		TextView price = (TextView) layout.findViewById(R.id.grid_price);
		this.imageLoader.displayImage(gridData.getImgurl(), img, this.options);
		title.setText(gridData.getGoodsname());
		price.setText(String.format("￥%.2f", gridData.getAgreementprice()));
		return layout;
	}

}
